<!--
公文跟踪
-->
<template>
  <view>
    <van-search
        v-model="title"
        placeholder="请输入关键字"
        @input="onSearch"
    >
    </van-search>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-empty description="暂无数据" v-show="emptyData"/>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载" :immediate-check="immediateCheck">
        <van-checkbox-group v-model="result">
          <van-swipe-cell  v-for="(item,index) in list" right-width="auto">
            <van-cell center :title="item.title" :label="'公文类型：'+item.bizType" :value="item.completeTime"
                      @click="toDetail(item.proInstId)" value-class="text-sm">
            </van-cell>
            <template #right>
              <view class="collection">
                <van-checkbox :name="item.proInstId" :key="item.proInstId" @click.native="collectionChange(item,index)">
                  <text v-if="isChecked(item.proInstId)" class="text-color">{{ collectionState }}</text>
                  <text v-else class="text-color">{{ inCollectionState }}</text>
                  <template #icon="props">
                    <img class="img-icon" :src="isChecked(item.proInstId) ? activeIcon : inactiveIcon" />
                  </template>
                </van-checkbox>
              </view>
              <van-button square type="info" v-if="item.canRevive == '1'" @click="review(item.proInstId)" text="恢复" />
              <van-button square type="info" v-else-if="item.copyTo == '1'" @click="withdraw(item.proInstId)" text="撤回" />
            </template>
          </van-swipe-cell>
        </van-checkbox-group>
      </van-list>
    </van-pull-refresh>
  </view>
</template>

<script>
export default {
  data() {
    return {
      //收藏
      result:[],
      collectionState:'已收藏',
      inCollectionState:'收藏',
      activeIcon: require('@/static/imgs/icon/icon-collection-active-white.png'),
      inactiveIcon: require('@/static/imgs/icon/icon-collection-white.png'),
      title:'',//搜索关键字
      emptyData:false,//是否显示暂无数据图片
      list: [],
      error: false,     // 错误状态
      loading: false,   // 加载状态
      finished: false,  // 是否加载
      refreshing: false,// 是否刷新
      page: 1,   		// 页数
      rows: 10, 		// 条数
      immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
    };
  },
  mounted() {
    this.onRefresh()
  },
  methods: {
    // 撤回
    withdraw(proInstId) {
      const _this = this
      _this.$http({
        url: _this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
        method: 'get',
        params: _this.$http.adornParams({
          'proInstId': proInstId,//发文详情返回的proInstId
          'systemKey': _this.cache.get('userInfo').systemKey,
          'systemFlag': _this.cache.get('userInfo').systemFlag,
        })
      }).then(({data}) => {
        if(data.code==200){
          _this.$http({
            url: _this.$http.adornUrl(`/caac/oa/component/workflow/workflowController/withdrawworkitemissign`),
            method: 'post',
            params: _this.$http.adornParams({
              'systemKey': _this.cache.get('userInfo').systemKey,
              'systemFlag': _this.cache.get('userInfo').systemFlag,
              'data': _this.$http.adornData({
                "wfmData": data.data,
                "isSigned": "1"
              })
            })
          }).then(({data}) => {
            if(data.code==200){
              _this.$toast(data.data)
              _this.onRefresh()
            } else {
              _this.$toast(data.message)
            }
          })
        }
      })
    },

    // 恢复
    review(proInstId) {
      const _this = this
      _this.$http({
        url: _this.$http.adornUrl(`/caac/oa/comm/comm/getreviewdata`),
        method: 'get',
        params: _this.$http.adornParams({
          'proInstId': proInstId,//发文详情返回的proInstId
          'systemKey': _this.cache.get('userInfo').systemKey,
          'systemFlag': _this.cache.get('userInfo').systemFlag,
        })
      }).then(({data}) => {
        if(data.code==200){
          _this.$http({
            url: _this.$http.adornUrl(`/caac/oa/component/workflow/workflowController/reviveprocess`),
            method: 'post',
            params: _this.$http.adornParams({
              'systemKey': _this.cache.get('userInfo').systemKey,
              'systemFlag': _this.cache.get('userInfo').systemFlag,
              'data': _this.$http.adornData({
                "proInstId": data.data.proInstId,
                "proDirId": data.data.proDirId,
                "configId": data.data.configId,
                "actDefId": data.data.actDefId,
                "workItemId": data.data.workitemId,
                "actInstId": data.data.actInstId
              })
            })
          }).then(({data}) => {
            if(data.code==200){
              _this.$toast(data.data.message)
              _this.onRefresh()
            } else {
              _this.$toast(data.message)
            }
          })
        }
      })
    },

    onSearch(){
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.onRefresh()
      },300)

    },
    // 判断是否选中
    isChecked(proInstId) {
      let coll = this.result.filter(t => t == proInstId)
      if(coll != null && coll.length > 0){
        return true
      } else {
        return false
      }
    },

    //收藏状态
    collectionChange(item, index){
      let flag = this.isChecked(item.proInstId)
      if(flag){
        // console.log('取消收藏')
        // 取消收藏
        this.cancelcCollection(item,index)
      }else{
        // console.log('收藏')
        // this.result.push(item.bizId)
        // 调用收藏接口
        this.addCollection(item,index)
      }

    },
    //添加收藏
    addCollection(item,index){
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/common/collection/addcollection`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'gwId':item.proInstId,
          'dirId':'root',
          'gwTitle':item.title,
          'gwType':item.code,
          'bizId':item.bizId
        })
      }).then(({data}) => {
        //{"code":200,"data":{"message":"收藏成功！"},"message":null}
        if(data.code == 200){
          this.$toast('收藏成功')
        }else{
          this.$toast(data.data.message)
        }
      })
    },
    //取消收藏
    cancelcCollection(item,index){
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/common/collection/cancelcollection`),
        method: 'delete',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'gwId':item.proInstId,
        })
      }).then(({data}) => {
        //{"code":200,"data":"取消收藏成功!","message":null}
        if(data.code == 200){
          this.$toast('取消收藏成功')
        }else{
          this.$toast(data.data.message)
        }
      })
    },
    // 下拉刷新
    onRefresh() {
      // 先清空数据
      this.list = []
      // 将没有更多的状态改成false
      this.finished = false
      // 默认加载第一页
      this.page = 1
      this.queryList()
      this.refreshing = false
    },
    load() {
      // 页数设置
      if(this.list.length === 0){
        this.page = 1
      } else {
        this.page++
      }
      // 将下拉刷新停止
      this.refreshing = false;
      setTimeout(() => {
        this.queryList()
      }, 500)
    },

    //查询公文
    queryList(){
      this.loading = true
      // 获取要显示的按钮
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mywork/gwwork/processfollow`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'title':this.title,
          'bizTypeCode':'',//公文类型
          'emergencyLevel':'',//仅显示紧急重要公文   勾了是true 没勾是空
          'page':this.page,
          'rows':this.rows
        })
      }).then(({data}) => {
        if(data.code==200){
          if(this.util.isNotEmpty(data.data)) {
            this.loading = false;
            if(this.page == 1) {
              this.list = data.data.rows
            } else {
              data.data.rows.forEach( t => this.list.push(t))
            }
            //收藏
            this.result=[]
            this.list.forEach((item)=>{
              if(item.isCollect == '1'){
                this.result.push(item.proInstId)
              }
            })

            if(this.list.length==0){
              this.emptyData = true
            }else{
              this.emptyData = false
            }
            // 判断当前页是否为最后一页
            if((this.page*this.rows) > data.data.total) {
              this.finished = true
            }
          } else {
            this.finished = true
          }
        }else{
          this.error = true
          console.log("出错data: " + JSON.stringify(data));
        }
      }).catch((err) => {
        this.error = true
      }).finally(() => {
        // 关闭加载状态
        this.loading = false
      })
    },

    //跳转详情code:发文类型，proInstId
    toDetail(proInstId){
      var from = "search"//如果是从查询里进详情，则不显示操作按钮
      // 会签件--hqj，发文（函）--glj_fw，呈批件--glj_cpj，发电--fd_hbglj，发电抄阅--hbgljfd，收文--glj_sw
      var _this = this
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
        method: 'get',
        params: this.$http.adornParams({
          'proInstId': proInstId,//发文详情返回的proInstId
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        if(data.code==200){
          _this.configCode = data.data.configCode
          console.log("公文类型==>"+_this.configCode)
          if(_this.configCode != ''){
            this.common.toAdmin(_this.configCode,proInstId,'search')
          }
        }else{
          this.$toast(data.message)
        }

      })
      // this.openwfneededdata(extendIdForRelate,proInstId)
    },

  },
};
</script>

<style scoped>
/deep/ .van-search__content {
  padding-left: 0;
}
/deep/ .van-cell {
  padding: 16rpx 30rpx;
}
/deep/ .van-dropdown-menu__bar {
  box-shadow: none;
}
/deep/ .van-dropdown-menu__bar--opened {
  z-index: 10;
}
/deep/ .van-search__action {
  margin: 0 8px;
  padding: 0 0px;
}
/*收藏样式*/
/deep/  .van-swipe-cell__right{
  display: flex;
  align-items: center;
}
.img-icon {
  height: 40rpx;
}
/*收藏样式*/

.collection {
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  height: 44px;
  margin: 0;
  font-size: 14px;
  line-height: 1.2;
  text-align: center;
  border-radius: 2px;
  cursor: pointer;
  -webkit-transition: opacity .2s;
  transition: opacity .2s;
  -webkit-appearance: none;
  color: #fff;
  background-color: #ee0a24;
  border: 1px solid #ee0a24;
  padding: 10px 2px;
}
.text-color{
  color: #FFFFFF;
}
</style>
